import request from "@/utils/request";
import CryptoJS from "crypto-js";
import {
  getAgent,
  setexamSetting,
  getexamSetting,
  delexamSetting,
  setlearnObject,
  getlearnObject,
  dellearnObject,
  setplaceObject,
  getplaceObject,
  delplaceObject,
  getGZ,
} from "@/utils/auth";
function encrypt(text) {
  var key = CryptoJS.enc.Utf8.parse("536e48ab5fb4521d"); //为了避免补位，直接用16位的秘钥
  var iv = CryptoJS.enc.Utf8.parse("cfab93be63f03c83"); //16位初始向量
  var encrypted = CryptoJS.AES.encrypt(text, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding,
  });
  return encrypted.toString();
}
// 通用url地址下载
export function downloadAll(url, name) {
  const link = document.createElement("a");
  let fileName =
    (name ? name : "下载文件") + url.substring(url.lastIndexOf("."));
  link.style.display = "none";
  if (getAgent() == 1) {
    link.href = url;
    link.download = fileName;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  } else {
    fetch(url)
      .then((res) => {
        return res.blob();
      })
      .then((blob) => {
        link.href = URL.createObjectURL(blob);
        link.download = fileName;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
  }
}
// ----------------登录----------------
//获取企业登录方式
export function Init(params) {
  return request({
    url: "/admin/platform/init",
    method: "get",
    params: params,
  });
}
/**
 * 登录
 * @param {array} data 请求参数
 */
export function login(data) {
  return request({
    url: "/index/Login/login",
    method: "post",
    data: encrypt(JSON.stringify(data)),
  });
}
/**
 * 创建登录二维码
 * @param {array} data 请求参数
 */
export function getqrcode(data) {
  return request({
    url: "/index/login/getqrcode",
    method: "post",
    data: data,
  });
}
/**
 *用户扫描服务号二维码登录
 * @param {array} params 请求参数
 */
export function scanresult(params) {
  return request({
    url: "/index/login/scanresult",
    method: "get",
    params: params,
  });
}
/**
 *用户绑定服务号
 * @param {array} params 请求参数
 */
export function bindservice(params) {
  return request({
    url: "/index/login/bindservice",
    method: "get",
    params: params,
  });
}
/**
 * 获取人员信息
 * @param {array} params 请求参数
 */
export function userinfo(params) {
  return request({
    url: "/index/user/userinfo",
    method: "get",
    params: params,
  });
}
/**
 * 退出登录
 * @param {array} data 请求参数
 */
export function logout(data) {
  return request({
    url: "index/Login/logout",
    method: "post",
    data: data,
  });
}

/**
 * 微信登录 获取个人信息
 * @param {array} data 请求参数
 */
export function wxlogin(data) {
  return request({
    url: "/index/login/unionid",
    method: "get",
    params: data,
    // headers:{
    //   'Content-Type': 'application/x-www-form-urlencoded'
    // },
  });
}
/**
 * 微信登录 获取微信appid
 * @param {array} data 请求参数
 */
export function wxinfo(data) {
  return request({
    url: "/index/login/wxinfo",
    method: "get",
    params: data,
    // headers:{
    //   'Content-Type': 'application/x-www-form-urlencoded'
    // },
  });
}
/**
 * 微信登录 获取微信appid
 * @param {array} data 请求参数
 */
export function getcodev2(data) {
  return request({
    url: "/index/login/getcodev2",
    method: "get",
    params: data,
    // headers:{
    //   'Content-Type': 'application/x-www-form-urlencoded'
    // },
  });
}
/**
 * 微信登录 获取微信accesstoken
 * @param {array} data 请求参数
 */
export function wxaccesstoken(data) {
  return request({
    url: "/index/login/accesstoken",
    method: "get",
    params: data,
    // headers:{
    //   'Content-Type': 'application/x-www-form-urlencoded'
    // },
  });
}
/**
 * 获取openid
 * @param {array} data 请求参数
 */
export function getopenid(data) {
  return request({
    url: "/index/login/getopenid",
    method: "get",
    params: data,
  });
}
/**
 * 微信浏览器登录 获取个人信息
 * @param {array} data 请求参数
 */
export function getcodeurl(params) {
  return request({
    url: "/index/login/getcodeuri",
    method: "get",
    params: params,
    // headers:{
    //   'Content-Type': 'application/x-www-form-urlencoded'
    // },
  });
}
/**
 * 绑定微信  通过unid
 * @param {array} data 请求参数
 */
export function binduser(data) {
  return request({
    url: "/index/login/binduser",
    method: "post",
    data: data,
  });
}
/**
 * 绑定微信  通过openid
 * @param {array} data 请求参数
 */
export function binduserOpenid(data) {
  return request({
    url: "/index/login/bindUserByOpenid",
    method: "post",
    data: data,
  });
}
/**
 * 个人信息 - 修改密码
 * @param {array} data 请求参数
 */
export function passwordEdit(data) {
  return request({
    url: "/index/user/userpwd",
    method: "post",
    data: data,
  });
}
/**
 * 个人信息 - 修改
 * @param {array} data 请求参数
 */
export function updateApply(data) {
  return request({
    url: "/index/user/updateApply",
    method: "post",
    data: data,
  });
}
/**
 * 专项练习-出题
 * @param {array} params 请求参数
 */
export function exerList(params) {
  return request({
    url: "/index/exer/list",
    method: "get",
    params: params,
  });
}
/**
 * 专项练习-学习记录
 * @param {array} params 请求参数
 */
export function exermylist(params) {
  return request({
    url: "/index/exer/mylist",
    method: "get",
    params: params,
  });
}
/**
 * 专项练习-题数记录
 * @param {array} params 请求参数
 */
export function getAnswered(params) {
  return request({
    url: "/index/exer/getAnswered",
    method: "get",
    params: params,
  });
}
/**
 * 专项练习-精选题库-查询试题数量
 * @param {array} params 请求参数
 */
export function getQcount(params) {
  return request({
    url: "index/exer/qcount",
    method: "get",
    params: params,
  });
}
/**
 * 专项练习-提交答题记录
 * @param {array} params 请求参数
 */
export function exerAdd(params, data) {
  return request({
    url: "/index/exer/done?exerid=" + params,
    method: "post",
    data: data,
  });
}
/**
 * 专项练习-收藏试题
 * @param {array} data 请求参数
 */
export function exerCollect(data) {
  return request({
    url: "/index/exer/collect",
    method: "post",
    data: data,
  });
}
/**
 * 专项练习-取消收藏试题
 * @param {array} data 请求参数
 */
export function exerdelCollect(data) {
  return request({
    url: "/index/exer/cancelcollect",
    method: "post",
    data: data,
  });
}

/**
 * 模拟考试-考试配置
 * @param {array} params 请求参数
 */
export function getSetting(params) {
  if (
    getexamSetting() &&
    getexamSetting() != "undefined" &&
    getexamSetting() != "null" 
  ) {
    // if( && JSON.parse(getexamSetting()).dateTime + 60000 <= new Date().getTime()
    //   JSON.parse(getexamSetting()).dateTime + 60000 <= new Date().getTime()){
    //     delexamSetting();
    // }
    // if(
    //   !JSON.parse(getexamSetting()).data.subject_id ||   JSON.parse(getexamSetting()).data.subject_id != params.subject_id){
    //     delexamSetting();
    // }
  }
  // getexamSetting() &&
    // getexamSetting() != "undefined" &&
    // getexamSetting() != "null"
  if (
    false
  ) {
    return new Promise((resolve, reject) => {
      resolve({
        code: 200,
        data: JSON.parse(getexamSetting()).data,
      });
    });
  } else {
    return new Promise((resolve, reject) => {
      request({
        url: "/index/mock/getsetting",
        method: "get",
        params: params,
      })
        .then((res) => {
          setexamSetting({...res.data,subject_id:params.subject_id});
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }
}
/**
 * 正式考试-出题
 * @param {array} params 请求参数
 */
export function examList(params) {
  return request({
    url: "/index/exam/start",
    method: "get",
    params: params,
  });
}
/**
 * 正式考试-考试时间
 * @param {array} params 请求参数
 */
export function checkScheme(params) {
  return request({
    url: "index/exam/jihua",
    method: "get",
    params: params,
  });
}
/**
 * 正式考试-我的试卷
 * @param {array} params 请求参数
 */
export function exammyList(params) {
  return request({
    url: "/index/exam/mylist",
    method: "get",
    params: params,
  });
}
/**
 * 模拟考试-单次考试错题
 * @param {array} params 请求参数
 */
export function getwrongbymockid(params) {
  return request({
    url: "/index/mock/getwrongbymockid",
    method: "get",
    params: params,
  });
}
/**
 * 模拟考试-清除错题库
 * @param {array} params 请求参数
 */
export function clearwrong(params) {
  return request({
    url: "/index/mock/clearwrong",
    method: "get",
    params: params,
  });
}
/**
 * 移出错题库
 * @param {array} params 请求参数
 */
export function removeWrong(params) {
  return request({
    url: "index/user/removeWrong",
    method: "post",
    data: params,
  });
}

/**
 * 正式考试-交卷
 * @param {array} params 请求参数
 */
export function examAdd(params, data) {
  return request({
    url: "/index/exam/done?examid=" + params.examid + "&gz=" + params.gz,
    method: "post",
    data: data,
  });
}
/**
 * 模拟考试-交卷
 * @param {array} params 请求参数
 */
export function resetwrongAdd(data) {
  return request({
    url: "/index/mock/resetwrong",
    method: "post",
    data: data,
  });
}

/**
 * 模拟考试-出题
 * @param {array} params 请求参数
 */
export function mockList(params) {
  return request({
    url: "/index/mock/start",
    method: "get",
    params: params,
  });
}

/**
 * 模拟考试-交卷
 * @param {array} params 请求参数
 */
export function mockAdd(params, data) {
  return request({
    url: "/index/mock/done?mockid=" + params.mockid + "&gz=" + params.gz,
    method: "post",
    data: data,
  });
}
/**
 * 模拟考试-我的试卷
 * @param {array} params 请求参数
 */
export function mocksjList(params) {
  return request({
    url: "/index/mock/mylist",
    method: "get",
    params: params,
  });
}
/**
 * 模拟考试-复卷
 * @param {array} params 请求参数
 */
export function mockRedo(params) {
  return request({
    url: "index/mock/redo",
    method: "get",
    params: params,
  });
}
/**
 * 模拟考试-我的错题
 * @param {array} params 请求参数
 */
export function mockWronglist(params) {
  return request({
    url: "/index/mock/wronglist",
    method: "get",
    params: params,
  });
}
/**
 * 模拟考试-我的收藏
 * @param {array} params 请求参数
 */
export function mycollectq(params) {
  return request({
    url: "index/mock/mycollectq",
    method: "get",
    params: params,
  });
}

/**
 * 模拟考试-收藏
 * @param {array} params 请求参数
 */
export function mockcollect(data) {
  return request({
    url: "/index/mock/collect",
    method: "post",
    data: data,
  });
}
/**
 * 模拟考试-取消收藏
 * @param {array} params 请求参数
 */
export function mockcancelcollect(data) {
  return request({
    url: "/index/mock/cancelcollect",
    method: "post",
    data: data,
  });
}
/**
 * 模拟考试-清除收藏题
 * @param {array} params 请求参数
 */
export function mockclearcollect(params) {
  return request({
    url: "/index/mock/clearcollect",
    method: "get",
    params: params,
  });
}

/**
 * 课程学习-科目表
 * @param {array} params 请求参数
 */
export function materialkmlist(params) {
  // setlearnObject  getlearnObject dellearnObject
  // if (
  //   getlearnObject() &&
  //   getlearnObject() != "undefined" &&
  //   getlearnObject() != "null" &&
  //   JSON.parse(getlearnObject()).dateTime + 10000 <= new Date().getTime() &&
  //   !JSON.parse(getlearnObject()).data &&
  //   !JSON.parse(getlearnObject()).data[0].params
  // ) {
  //   dellearnObject();
  // }
  // if (
  //   getlearnObject() &&
  //   getlearnObject() != "undefined" &&
  //   getlearnObject() != "null" &&
  //   JSON.parse(getlearnObject()).data[0] &&
  //   JSON.parse(getlearnObject()).data[0].params &&
  //   (params.msid != JSON.parse(getlearnObject()).data[0].params.msid ||
  //     params.applyid != JSON.parse(getlearnObject()).data[0].params.applyid ||
  //     params.cate != JSON.parse(getlearnObject()).data[0].params.cate)
  // ) {
  //   dellearnObject();
  // }
  // if (
  //   getlearnObject() &&
  //   getlearnObject() != "undefined" &&
  //   getlearnObject() != "null" &&
  //   JSON.parse(getlearnObject()).data.length == 0
  // ) {
  //   dellearnObject();
  // }
  // if (
  //   getlearnObject() &&
  //   getlearnObject() != "undefined" &&
  //   getlearnObject() != "null"
  // ) {
  //   return new Promise((resolve, reject) => {
  //     resolve({
  //       code: 200,
  //       data: JSON.parse(getlearnObject()).data,
  //     });
  //   });
  // } else {
    
  // }
  return new Promise((resolve, reject) => {
    request({
      url: "/index/material/mslist",
      method: "get",
      params: params,
    })
      .then((res) => {
        if (res.data[0]) {
          res.data[0].params = params;
        }
        setlearnObject(res.data);
        // let outTime = setTimeout(() => {
        //   dellearnObject()
        //   clearTimeout(outTime)
        // }, 3000);
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
}
/**
 * 课程学习-课程表
 * @param {array} params 请求参数
 */
export function materialList(params) {
  return request({
    url: "/index/material/list",
    method: "get",
    params: params,
  });
}
/**
 * 课程学习-收藏
 * @param {array} data 请求参数
 */
export function materialCollect(data) {
  return request({
    url: "/index/material/collect",
    method: "post",
    data: data,
  });
}
/**
 * 课程学习-取消收藏
 * @param {array} data 请求参数
 */
export function materialCancelcollect(data) {
  return request({
    url: "/index/material/cancelcollect",
    method: "post",
    data: data,
  });
}

/**
 * 课程学习-添加学习记录
 * @param {array} data 请求参数
 */
export function materialaddlog(data) {
  return request({
    url: "/index/material/addlog",
    method: "post",

    // data:obj,
    // data:data,
    data: encrypt(JSON.stringify(data)),
  });
}
/**
 * 课程学习-查询学习记录
 * @param {array} params 请求参数
 */
export function materiallearnlog(params) {
  return request({
    url: "/index/material/learnlog",
    method: "get",
    params: params,
  });
}
/**
 * 课程学习-收藏教材表
 * @param {array} params 请求参数
 */
export function collectlist(params) {
  return request({
    url: "index/material/collectlist",
    method: "get",
    params: params,
  });
}

/**
 * 首页我的错题
 * @param {array} params 请求参数
 */
export function mywrong(params) {
  return request({
    url: "/index/user/mywrong",
    method: "get",
    params: params,
  });
}

/**
 * 首页我的错题  报存
 * @param {array} data 请求参数
 */
export function resetwrong(data) {
  return request({
    url: "/index/user/resetwrong",
    method: "post",
    data: data,
  });
}

/**
 * 首页我的收藏
 * @param {array} params 请求参数
 */
export function mycollect(params) {
  return request({
    url: "index/user/mycollect",
    method: "get",
    params: params,
  });
}
/**
 * 首页我的收藏
 * @param {array} params 请求参数
 */
export function myTj(params) {
  return request({
    url: "index/user/tj",
    method: "get",
    params: params,
  });
}
/**
 * 随堂练习 出题
 * @param {array} params 请求参数
 */
export function quizStart(params) {
  return request({
    url: "index/quiz/start",
    method: "get",
    params: params,
  });
}
/**
 * 视频练题 出题
 * @param {array} params 请求参数
 */
export function videoPractice(params) {
  return request({
    url: "index/examMaterial/list",
    method: "get",
    params: params,
  });
}
/**
 * 视频练题 设置
 * @param {array} params 请求参数
 */
export function gzExamSetting(params) {
  return request({
    url: "index/ExamMaterial/gzExamSetting",
    method: "get",
    params: params,
  });
}
/**
 * 随堂练习 考试记录列表
 * @param {array} params 请求参数
 */
export function quizMylist(params) {
  return request({
    url: "index/quiz/mylist",
    method: "get",
    params: params,
  });
}
/**
 * 随堂练习 考试记录列表
 * @param {array} params 请求参数
 */
export function quizRedo(params) {
  return request({
    url: "index/quiz/redo",
    method: "get",
    params: params,
  });
}
/**
 * 随堂练习 交卷
 * @param {array} params 请求参数
 */
export function quizDone(params, data) {
  return request({
    url: "/index/quiz/done?quizid=" + params.quizid + "&gz=" + params.gz,
    method: "post",
    data: data,
  });
}
/**
 * 视频练习 交卷
 * @param {array} params 请求参数
 */
export function videoPracticeDone(params, data) {
  return request({
    url:
      "/index/examMaterial/done?quizid=" + params.quizid + "&gz=" + params.gz,
    method: "post",
    data: data,
  });
}

/**
 * 学时记录接口
 * @param {array} params 请求参数
 */
export function userlearn(params) {
  return request({
    url: "/index/user/userlearn",
    method: "get",
    params: params,
  });
}

//人脸识别  /index/face/init
export function faceInit(data) {
  return request({
    url: "/index/face/init",
    method: "post",
    data: data,
  });
}

//人脸照片上传
export function faceupload(data) {
  return request({
    url: "/index/user/upload",
    method: "post",
    data: data,
  });
}
//人脸对比
export function facecompare(data) {
  return request({
    url: "/index/face/compare",
    method: "post",
    data: data,
  });
}

//微信支付下单
export function applyPay(data) {
  return request({
    url: "/index/apply/mp",
    method: "get",
    params: data,
  });
}
//微信支付下单
export function applyPayUp(data) {
  return request({
    url: "/index/apply/mpo",
    method: "get",
    params: data,
  });
}

//搜题功能
export function questionsearch(data) {
  return request({
    url: "/index/question/search",
    method: "get",
    params: data,
  });
}

//二维码支付
export function maPay(data) {
  return request({
    url: "/index/apply/mpnative",
    method: "get",
    params: data,
  });
}

//生成二维码
export function qrcode(data) {
  return request({
    url: "/admin/AdminTool/qrcode",
    method: "post",
    data,
  });
}

//支付订单查询
export function PayList(data) {
  return request({
    url: "/index/apply/findo",
    method: "get",
    params: data,
  });
}
//人工组卷
//人工组卷 试卷查询
export function paperlist(data) {
  return request({
    url: "/index/mock/paperlist",
    method: "get",
    params: data,
  });
}
//人工组卷 模拟考试
export function zujuanstart(data) {
  return request({
    url: "/index/mock/zujuanstart",
    method: "get",
    params: data,
  });
}
//人工组卷 正式考试
export function zujuanstartexam(data) {
  return request({
    url: "/index/exam/zujuanstart",
    method: "get",
    params: data,
  });
}
//人工组卷 模拟考试交卷
export function zujuandone(data) {
  return request({
    url: "/index/mock/zujuandone",
    method: "post",
    data,
  });
}
//人工组卷 正式考试交卷
export function zujuandoneexam(data) {
  return request({
    url: "/index/exam/zujuandone",
    method: "post",
    data,
  });
}

//自行修改培训科目
export function changesid(data) {
  return request({
    url: "/index/apply/changesid",
    method: "post",
    data,
  });
}

//捕获异常
export function console(data) {
  return request({
    url: "/index/index/console",
    method: "post",
    data,
  });
}

//发票申请
export function applyinvoice(data) {
  return request({
    url: "/index/invoice/create",
    method: "post",
    data,
  });
}

//获取报名列表字段

export function applyList(params) {
  return request({
    url: "/admin/ApplySelectedField/list",
    method: "get",
    params: params,
  });
}
/**
 * 位置信息请求
 * @param {array} params 请求参数
 */
export function regionList(params) {
  // setplaceObject getplaceObject delplaceObject
  return request({
    url: "admin/region/regionList",
    method: "get",
    params: params,
  });
}
/**
 * 上传
 * @param {array} params 请求参数
 */
export function uploadFile(params) {
  return request({
    url: "/admin/MemberApply/upload",
    method: "post",
    data: params,
  });
}
/**
 * 知识点
 * @param {array} params 请求参数
 */
export function chapter(params) {
  return request({
    url: "/index/chapter",
    method: "get",
    params: params,
  });
}

export function feedbackQuestion(params) {
  return request({
    url: "/index/feedbackQuestion/create",
    method: "post",
    data: params,
  });
}
export function feedbackList(params) {
  return request({
    url: "/index/feedbackQuestion/index",
    method: "get",
    params: params,
  });
}
//教师评价
export function evaluationTeacherLog(params) {
  return request({
    url: "/index/evaluationTeacherLog/create",
    method: "post",
    data: params,
  });
}
//教师评价 - 列表
export function evaluationTeacherList(params) {
  return request({
    url: "/index/evaluationTeacherLog",
    method: "get",
    params: params,
  });
}
//教学评价
export function evaluationLog(params) {
  return request({
    url: "/index/evaluationLog/create",
    method: "post",
    data: params,
  });
}
//教学评价 - 列表
export function evaluationLogList(params) {
  return request({
    url: "/index/evaluationLog",
    method: "get",
    params: params,
  });
}
//课程列表vid
export function aliVodInfo(params) {
  return request({
    url: "/index/aliVod",
    method: "get",
    params: params,
  });
}
//课程列表url
export function playInfo(params) {
  return request({
    url: "/index/aliVod/playInfo",
    method: "get",
    params: params,
  });
}
